var selectedItem1 = null;
var selectedItem2 = null;
var indexSelected = null;

function setListCritics(){
	//document.getElementById("cmbPerson1").selectedIndex
	var cmb1 = document.getElementById("cmbPerson1");
	initCmb(cmb1);
	selectedItem1 = cmb1.options[cmb1.selectedIndex];
	var cmb2 = document.getElementById("cmbPerson2");
	initCmb(cmb2);
	var cmbRec = document.getElementById("cmbRecomendacion");
	initCmb(cmbRec);
	var cmbtopP = document.getElementById("cmbTopMatches");
	initCmb(cmbtopP);
	var cmbtopPelis = document.getElementById("cmbTopMatchesPelis");
	initCmbPelis(cmbtopPelis);
	
}

function initCmbPelis(target){

		target.options[0] = new Option("Selected...","","defauldSelected");
		target.options[1] = new Option("Lady in the Water","Lady in the Water");
		target.options[2] = new Option("Snakes on a Plane","Snakes on a Plane");
		target.options[3] = new Option("Just My Luck","Just My Luck");
		target.options[4] = new Option("Superman Returns","Superman Returns");
		target.options[5] = new Option("You, Me and Dupree","You, Me and Dupree");
		target.options[6] = new Option("The Night Listener","The Night Listener");
	
}

function initCmb(target){
/*
	target.options[0] = new Option("Selected...","","defauldSelected");
	target.options[1] = new Option("Lady in the Water","Lady in the Water");
	target.options[2] = new Option("Snakes on a Plane","Snakes on a Plane");
	target.options[3] = new Option("ust My Luck","ust My Luck");
	target.options[4] = new Option("Superman Returns","Superman Returns");
	target.options[5] = new Option("You, Me and Dupree","You, Me and Dupree");
	target.options[6] = new Option("The Night Listener","The Night Listener");
	target.options[7] = new Option("Lisa Rose","Lisa Rose");
*/
	target.options[0] = new Option("Selected...","","defauldSelected");
	target.options[1] = new Option("Lisa Rose","Lisa Rose");
	target.options[2] = new Option("Gene Seymour","Gene Seymour");
	target.options[3] = new Option("Michael Phillips","Michael Phillips");
	target.options[4] = new Option("Claudia Puig","Claudia Puig");
	target.options[5] = new Option("Mick LaSalle","Mick LaSalle");
	target.options[6] = new Option("Jack Matthews","Jack Matthews");
	target.options[7] = new Option("Toby","Toby");
}

function removeItem(target,index){
	var cmbTarget = document.getElementById(target);
	initCmb(cmbTarget);
	if( index != 0 ){
		cmbTarget.options[index] = null;
	}
}

function validate(){
	var val = false;
	if( document.getElementById("cmbPerson1").indexSelected != 0 && document.getElementById("cmbPerson2").indexSelected != 0 ){
		val = true;
	}
	return val;	
}

$(document).ready(function(){
	$('#btnDoSearch').click(function (){
		$("#details_popup_content").html("&nbsp;");
		if ( document.getElementById("cmbPerson1").indexSelected == 0 || document.getElementById("cmbPerson2").indexSelected == 0 ){
			return ;	
		}else{
			var strUrl = 'ci.do';
			$.ajax({
				type: "POST",
				url: strUrl,
				data: $('#frmCriticos').serialize(),
				success: function(msg){
					/*alert("success"+msg);*/
					$("#details_popup_content").html(msg);
					fnShowDetails();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#msgError").html("Seleecione un critico en cada unos de los combos");
					$("#msgError").fadeIn().animate({opacity: 1.0}, 3000);
				}
			});
			return false;
		}
	});
	
	$('#btnDoSearchRecomendacion').click(function (){
		$("#details_popup_content").html("&nbsp;");
		if ( document.getElementById("cmbRecomendacion").indexSelected == 0 ){
			return ;	
		}else{
			var strUrl = 'recomendation.do';
			$.ajax({
				type: "POST",
				url: strUrl,
				data: $('#frmRecomendacion').serialize(),
				success: function(msg){
					/*alert("success"+msg);*/
					$("#details_popup_content").html(msg);
					fnShowDetails();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#msgError").html("Seleecione un critico para la Recomendacion");
					$("#msgError").fadeIn().animate({opacity: 1.0}, 3000);
				}
			});
			return false;
		}
	});
	
	$('#btnDoSearchTopMatches').click(function (){
		$("#details_popup_content").html("&nbsp;");
		if ( document.getElementById("cmbTopMatches").indexSelected == 0 ){
			return ;	
		}else{
			var strUrl = 'topMatchesPer.do';
			$.ajax({
				type: "POST",
				url: strUrl,
				data: $('#frmTopMatches').serialize(),
				success: function(msg){
					/*alert("success"+msg);*/
					$("#details_popup_content").html(msg);
					fnShowDetails();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#msgError").html("Seleecione un critico para TopMatches");
					$("#msgError").fadeIn().animate({opacity: 1.0}, 3000);
				}
			});
			return false;
		}
	});
	
	$('#btnDoSearchTopMatchesPelis').click(function (){
		$("#details_popup_content").html("&nbsp;");
		if ( document.getElementById("cmbTopMatchesPelis").indexSelected == 0 ){
			return ;	
		}else{
			var strUrl = 'topMatchesPeliculas.do';
			$.ajax({
				type: "POST",
				url: strUrl,
				data: $('#frmTopMatchesPelis').serialize(),
				success: function(msg){
					/*alert("success"+msg);*/
					$("#details_popup_content").html(msg);
					fnShowDetails();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#msgError").html("Seleecione un critico para TopMatches Peliculas");
					$("#msgError").fadeIn().animate({opacity: 1.0}, 3000);
				}
			});
			return false;
		}
	});

});


function  modalOpen (dialog) {
	dialog.overlay.fadeIn('fast', function () {
		dialog.container.fadeIn('fast', function () {
			dialog.data.hide().slideDown('fast');
		});
	});
}

function  simplemodal_close (dialog) {
	dialog.data.fadeOut('fast', function () {
		dialog.container.hide('fast', function () {
			dialog.overlay.slideUp('fast', function () {
				$.modal.close();
			});
		});
	});
}

function fnShowDetails(){
	$("#details_popup").modal({onOpen: modalOpen, onClose: simplemodal_close});
}